package com.macro.mall.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.AgentDao;
import com.macro.mall.dto.Agent;
import com.macro.mall.dto.AgentExample;
import com.macro.mall.dto.AgentRequest;
import com.macro.mall.service.AgentService;
import com.macro.mall.util.example.ExampleUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author HuangZiy
 * @version 8.0
 * @date 2021/4/26 22:43
 */
@Service
public class AgentServiceImpl implements AgentService {
    @Autowired
    private AgentDao agentDao;

    @Override
    public CommonResult<List<Agent>> getAgent(AgentRequest request) {
        AgentExample agentExample = ExampleUtil.getExampleByAnnotation(request, AgentExample.class);
        List<Agent> agents = agentDao.selectByExample(agentExample);
        if (CollectionUtils.isEmpty(agents)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(agents);
    }

    @Override
    public CommonResult<String> addAgent(AgentRequest request) {
        Agent agent = new Agent();
        BeanUtils.copyProperties(request, agent);
        int i = agentDao.insertSelective(agent);
        if (i <= 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success("插入成功");
    }

    @Override
    public CommonResult<String> updateAgent(AgentRequest request) {
        Agent agent = new Agent();
        BeanUtils.copyProperties(request, agent);
        int i = agentDao.updateByPrimaryKeySelective(agent);
        if (i <= 0) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @Override
    public CommonResult<String> deleteAgent(Integer id) {
        int i = agentDao.deleteByPrimaryKey(id);
        if (i <= 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }
}
